import top.malaoshi.data.DataColumn;
import top.malaoshi.data.DataRow;
import top.malaoshi.data.DataSet;
import top.malaoshi.data.DataTable;
import top.malaoshi.util.Out;

import java.io.IOException;

/**
 * 测试数据添加、遍历
 */
public class S7json_dataset {

    static DataTable createTable(){
        DataTable dt = new DataTable("t_user");

        dt.columns().add("id");
        dt.columns().add("username", DataColumn.Type.TEXT);
        dt.columns().add("password", DataColumn.Type.TEXT);
        dt.columns().add("age", DataColumn.Type.INT);
        dt.columns().add("height", DataColumn.Type.DOUBLE);

        return dt;
    }
    static DataTable createTable2(){
        DataTable dt = new DataTable("t_dept");

        dt.columns().add("id", DataColumn.Type.INT);
        dt.columns().add("name", DataColumn.Type.TEXT);
        dt.columns().add("loc", DataColumn.Type.TEXT);

        return dt;
    }
    public static void main(String[] args) {

        DataTable dt = createTable();

        dt.rows().add(new Object[]{1,"lilei","123456",20,1.83});
        DataRow drHmm = dt.rows().add(new Object[]{2, "韩梅梅", "666", 22,1.65});

        // 创建 DataRnow
        DataRow dataRow = dt.newRow();
        dt.rows().add(dataRow);
//        通过索引设置数据
        dataRow.setValue( 0,3 );
        dataRow.setValue( 1,"lucy" );
        dataRow.setValue( 2,"333" );
//        dataRow.setValue( 3,22);
        dataRow.setValue( 4,1.73 );

        // 创建 DataRnow
        DataRow dataRow2 = dt.newRow();
        dt.rows().add(dataRow2);
//      通过列名设置数据
        dataRow2.setValue("id",4);
        dataRow2.setValue("username","lili");
//        dataRow2.setValue("password","555");
        dataRow2.setValue("age","28");
        dataRow2.setValue("height",1.65);


        print(dt);

        DataTable dept = createTable2();
        dept.rows().add(new Object[]{1,"开发部","北京"});
        dept.rows().add(new Object[]{2,"营销部","上海"});
        dept.rows().add(new Object[]{3,"测试部","大连"});

        DataSet ds = new DataSet("测试");
        ds.tables().add(dt);
        ds.tables().add(dept);
        try {
            ds.writeJson("dataset.txt");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    static void print(DataTable dt){

        Out.println("表名：",dt.getName());

        Out.println("DataRow根据索引获取数据：");
        for(int i = 0,size = dt.rows().count(); i<size ; i++){
            DataRow row = dt.rows().get(i);
            Out.print("",row.getValue(0));
            Out.print(",",row.getValue(1));
            Out.print(",",row.getValue(2));
            Out.print(",",row.getValue(3));
            Out.println(",",row.getValue(4));
        }
    }

}
